package com.irdstudio.tdp.console.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.irdstudio.sdk.beans.core.util.KeyUtil;
import com.irdstudio.tdp.console.dao.IsrvInoutInfoDao;
import com.irdstudio.tdp.console.dao.domain.IsrvInoutInfo;
import com.irdstudio.tdp.console.service.vo.IsrvInoutInfoVO;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.irdstudio.tdp.console.service.facade.OsrvIsrvEvalService;
import com.irdstudio.tdp.console.dao.OsrvIsrvEvalDao;
import com.irdstudio.tdp.console.dao.domain.OsrvIsrvEval;
import com.irdstudio.tdp.console.service.vo.OsrvIsrvEvalVO;
import com.irdstudio.sdk.beans.core.base.FrameworkService;
/**
 * <p>ServiceImpl: 外部模型服务调用赋值				<p>
 * @author zjj
 * @date 2021-03-16
 */
@Service("osrvIsrvEvalServiceImpl")
public class OsrvIsrvEvalServiceImpl implements OsrvIsrvEvalService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(OsrvIsrvEvalServiceImpl.class);

	@Autowired
	private OsrvIsrvEvalDao osrvIsrvEvalDao;

	@Autowired
	private IsrvInoutInfoDao isrvInoutInfoDao;
	
	/**
	 * 新增操作
	 */
	public int insertOsrvIsrvEval(OsrvIsrvEvalVO inOsrvIsrvEvalVo) {
		logger.debug("当前新增数据为:"+ inOsrvIsrvEvalVo.toString());
		int num = 0;
		try {
			OsrvIsrvEval osrvIsrvEval = new OsrvIsrvEval();
			beanCopy(inOsrvIsrvEvalVo, osrvIsrvEval);
			num = osrvIsrvEvalDao.insertOsrvIsrvEval(osrvIsrvEval);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(OsrvIsrvEvalVO inOsrvIsrvEvalVo) {
		logger.debug("当前删除数据条件为:"+ inOsrvIsrvEvalVo);
		int num = 0;
		try {
			OsrvIsrvEval osrvIsrvEval = new OsrvIsrvEval();
			beanCopy(inOsrvIsrvEvalVo, osrvIsrvEval);
			num = osrvIsrvEvalDao.deleteByPk(osrvIsrvEval);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inOsrvIsrvEvalVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(OsrvIsrvEvalVO inOsrvIsrvEvalVo) {
		logger.debug("当前修改数据为:"+ inOsrvIsrvEvalVo.toString());
		int num = 0;
		try {
			OsrvIsrvEval osrvIsrvEval = new OsrvIsrvEval();
			beanCopy(inOsrvIsrvEvalVo, osrvIsrvEval);
			num = osrvIsrvEvalDao.updateByPk(osrvIsrvEval);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inOsrvIsrvEvalVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public OsrvIsrvEvalVO queryByPk(OsrvIsrvEvalVO inOsrvIsrvEvalVo) {
		
		logger.debug("当前查询参数信息为:"+ inOsrvIsrvEvalVo);
		try {
			OsrvIsrvEval queryOsrvIsrvEval = new OsrvIsrvEval();
			beanCopy(inOsrvIsrvEvalVo, queryOsrvIsrvEval);
			OsrvIsrvEval queryRslOsrvIsrvEval = osrvIsrvEvalDao.queryByPk(queryOsrvIsrvEval);
			if (Objects.nonNull(queryRslOsrvIsrvEval)) {
				OsrvIsrvEvalVO outOsrvIsrvEvalVo = beanCopy(queryRslOsrvIsrvEval, new OsrvIsrvEvalVO());
				logger.debug("当前查询结果为:"+ outOsrvIsrvEvalVo.toString());
				return outOsrvIsrvEvalVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据 列表为空 并根据服务列表进行初始化
	 */
	@SuppressWarnings("unchecked")
	public List<OsrvIsrvEvalVO> queryAllOwnerCheckInit(String appModelId, OsrvIsrvEvalVO osrvIsrvEvalVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<OsrvIsrvEvalVO> list = null;
		try {
			List<OsrvIsrvEval> osrvIsrvEvals = osrvIsrvEvalDao.queryAllOwnerByPage(osrvIsrvEvalVo);
			// 列表为空  判断是为新添加领域表格行的服务调用 根据 服务参数 初始
			if (CollectionUtils.isEmpty(osrvIsrvEvals)) {
				// 复制 isrv_inout_info的参数到osrv_isrv_eval
				IsrvInoutInfoVO inoutParam = new IsrvInoutInfoVO();
				inoutParam.setAppModelId(appModelId);
				List<IsrvInoutInfo> srcInoutList = isrvInoutInfoDao.queryAllOwner(inoutParam);
				if (CollectionUtils.isNotEmpty(srcInoutList)) {
					List<OsrvIsrvEval> targetInoutList = new ArrayList<>(srcInoutList.size());
					for (IsrvInoutInfo srcIn : srcInoutList) {
						OsrvIsrvEval tarIn = new OsrvIsrvEval();

						tarIn.setRecordKeyid(KeyUtil.createUUIDKey());
						tarIn.setNodeId(osrvIsrvEvalVo.getNodeId());
						tarIn.setInFieldCode(srcIn.getParamFieldCode());
						tarIn.setInFieldName(srcIn.getParamFieldName());
						tarIn.setIoType(srcIn.getIoType());
						tarIn.setMustNeed(srcIn.getMustNeed());
						tarIn.setEvalSource("02");
						tarIn.setEvalContent(srcIn.getParamFieldCode());
						tarIn.setEvalDesc(srcIn.getParamFieldName());
						targetInoutList.add(tarIn);
					}
					osrvIsrvEvalDao.batchInsertOsrvIsrvEvals(targetInoutList);
					list = (List<OsrvIsrvEvalVO>) beansCopy(targetInoutList, OsrvIsrvEvalVO.class);
				}
			}
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ osrvIsrvEvals.size());
			pageSet(osrvIsrvEvals, osrvIsrvEvalVo);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}

	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<OsrvIsrvEvalVO> queryAllOwner(OsrvIsrvEvalVO osrvIsrvEvalVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<OsrvIsrvEvalVO> list = null;
		try {
			List<OsrvIsrvEval> osrvIsrvEvals = osrvIsrvEvalDao.queryAllOwnerByPage(osrvIsrvEvalVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ osrvIsrvEvals.size());
			pageSet(osrvIsrvEvals, osrvIsrvEvalVo);
			list = (List<OsrvIsrvEvalVO>) beansCopy(osrvIsrvEvals, OsrvIsrvEvalVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<OsrvIsrvEvalVO> queryAllCurrOrg(OsrvIsrvEvalVO osrvIsrvEvalVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<OsrvIsrvEval> osrvIsrvEvals = osrvIsrvEvalDao.queryAllCurrOrgByPage(osrvIsrvEvalVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+osrvIsrvEvals.size());
		List<OsrvIsrvEvalVO> list = null;
		try {
			pageSet(osrvIsrvEvals, osrvIsrvEvalVo);
			list = (List<OsrvIsrvEvalVO>) beansCopy(osrvIsrvEvals, OsrvIsrvEvalVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<OsrvIsrvEvalVO> queryAllCurrDownOrg(OsrvIsrvEvalVO osrvIsrvEvalVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<OsrvIsrvEval> osrvIsrvEvals = osrvIsrvEvalDao.queryAllCurrDownOrgByPage(osrvIsrvEvalVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ osrvIsrvEvals.size());
		List<OsrvIsrvEvalVO> list = null;
		try {
			pageSet(osrvIsrvEvals, osrvIsrvEvalVo);
			list = (List<OsrvIsrvEvalVO>) beansCopy(osrvIsrvEvals, OsrvIsrvEvalVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}
}
